<script setup>
import { ref } from 'vue'
import { useUserStore } from '@/stores/user'
import { submitOrderService } from '@/apis/detail'
import dayjs from 'dayjs'

const UserStore = useUserStore()

// 获取表单
const formRef = ref(null)

// 控制对话框的显示
const dialogVisible = ref(false)

// 用户预约信息
const formModel = ref({
  name: '',
  phone: '',
  email: '',
  datetime: '',
  remark: ''
})

// 将用户ID存入预约信息
formModel.value.user_id = UserStore.UserList.id

const rules = {
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    {
      pattern: /^(?:[\u4e00-\u9fa5·]{2,16})$/,
      message: '请输入正确的姓名',
      trigger: 'blur'
    }
  ],
  phone: [
    { required: true, message: '请输入联系电话', trigger: 'blur' },
    {
      pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/,
      message: '请输入正确的手机号码',
      trigger: 'blur'
    }
  ],
  email: [
    { required: true, message: '请输入电子邮箱', trigger: 'blur' },
    {
      pattern:
        /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
      message: '请输入正确的邮箱',
      trigger: 'blur'
    }
  ],
  datetime: [
    { required: true, message: '请选择看房日期和时间', trigger: 'blur' }
  ]
}

// 打开对话框
const openDialog = (value) => {
  dialogVisible.value = true
  formModel.value.room_id = value
}

// 点击确认时提交预约信息
const onSubmit = async () => {
  await formRef.value.validate()

  // 对预约日期时间进行处理
  if (formModel.value.datetime) {
    formModel.value.datetime = dayjs(formModel.value.datetime).format()
  }

  // 提交用户预约信息
  await submitOrderService(formModel.value)
  ElMessage.success('提交成功')
  formModel.value = {}
  dialogVisible.value = false
}

// 对外暴露打开对话框的方法
defineExpose({
  openDialog
})
</script>

<template>
  <el-dialog v-model="dialogVisible" style="border-radius: 10px">
    <template #title>
      <h1>看房预约信息表</h1>
    </template>
    <el-form
      :model="formModel"
      autocomplete="off"
      label-width="auto"
      size="large"
      :rules="rules"
      ref="formRef"
      style="margin: 20px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input v-model="formModel.name" placeholder="请输入姓名"></el-input>
      </el-form-item>

      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="formModel.phone"
          placeholder="请输入联系电话"
        ></el-input>
      </el-form-item>

      <el-form-item label="电子邮箱" prop="email">
        <el-input
          v-model="formModel.email"
          placeholder="请输入电子邮箱"
        ></el-input>
      </el-form-item>

      <el-form-item label="备注">
        <el-input
          v-model="formModel.remark"
          placeholder="请输入备注"
        ></el-input>
      </el-form-item>

      <el-form-item label="看房日期" prop="datetime">
        <el-date-picker
          v-model="formModel.datetime"
          type="datetime"
          placeholder="请选择日期时间"
          :size="large"
        ></el-date-picker>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="onSubmit">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped></style>
